package com.hairline.hairlinemarket.service.impl;

import com.hairline.hairlinemarket.bean.MarketAdmin;
import com.hairline.hairlinemarket.bean.MarketCategory;
import com.hairline.hairlinemarket.bean.MarketGoods;
import com.hairline.hairlinemarket.bean.MarketUser;
import com.hairline.hairlinemarket.bean.common.BaseRespVo;
import com.hairline.hairlinemarket.service.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

/**
 * Author:杨小凯
 * Date:2023-04-20 18:43
 * Description:<描述>
 */
@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    private AdService adService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CouponService couponService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private SystemService systemService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private TopicService topicService;

    @Override
    public Map<String, Object> selectIndex() {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        // 之前后台管理中的广告管理
        Callable<List> bannerListCallable = () -> adService.queryIndex();
        // 之前后台管理中的类目管理
        Callable<List> channelListCallable = () -> categoryService.queryChannel();
        // 获取优惠券列表
        Callable<List> couponListCallable;

        Integer userId;
        // 判断是否是登录状态
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            MarketUser principal = (MarketUser) subject.getPrincipal();
            // 拿到了个人信息，也包含id ，就可以去做其他业务了
            userId = principal.getId();
        } else {
            userId = null;
        }
        if (userId == null) {
            //如果是未登录，就查询所有的优惠券中的前三条
            couponListCallable = () -> couponService.queryList(0, 3);
        } else {
            //如果是已经登录，就查询当前用户还未领取的前三张优惠券
            // userId
            couponListCallable = () -> couponService.queryAvailableList(userId, 0, 3);
        }

        // 查询新添加的商品Goods，之后后台管理中的商品管理，商品上架时有选项是否是新品，根据添加时间做排序，limit获取market_system表中的值
        Callable<List> newGoodsListCallable = () -> goodsService.queryByNew(0, systemService.getNewLimit());
        // 查询热门商品Goods，之后后台管理中的商品管理，商品上架时有选项是否是热卖，根据添加时间做排序，limit获取market_system表中的值
        Callable<List> hotGoodsListCallable = () -> goodsService.queryByHot(0, systemService.getHotLimit());
        // 之后后台管理中的制造商管理，根据添加时间做排序，limit获取market_system表中的值
        Callable<List> brandListCallable = () -> brandService.query(0, systemService.getBrandLimit());
        // 之前后台管理中的专题管理，根据添加时间做排序，limit获取market_system表中的值
        Callable<List> topicListCallable = () -> topicService.queryList(0, systemService.getTopicLimit());

        // 查询类目category以及类目下的商品goods，limit获取market_system表中的值
        Callable<List> floorGoodsListCallable = this::getCategoryList;

        FutureTask<List> bannerTask = new FutureTask<>(bannerListCallable);
        FutureTask<List> channelTask = new FutureTask<>(channelListCallable);
        FutureTask<List> couponListTask = new FutureTask<>(couponListCallable);
        FutureTask<List> newGoodsListTask = new FutureTask<>(newGoodsListCallable);
        FutureTask<List> hotGoodsListTask = new FutureTask<>(hotGoodsListCallable);
        FutureTask<List> brandListTask = new FutureTask<>(brandListCallable);
        FutureTask<List> topicListTask = new FutureTask<>(topicListCallable);
        FutureTask<List> floorGoodsListTask = new FutureTask<>(floorGoodsListCallable);

        executorService.submit(bannerTask);
        executorService.submit(channelTask);
        executorService.submit(couponListTask);
        executorService.submit(newGoodsListTask);
        executorService.submit(hotGoodsListTask);
        executorService.submit(brandListTask);
        executorService.submit(topicListTask);
        executorService.submit(floorGoodsListTask);
        Map<String, Object> entity = new HashMap<>();
        try {
            entity.put("banner", bannerTask.get());
            entity.put("channel", channelTask.get());
            entity.put("couponList", couponListTask.get());
            entity.put("newGoodsList", newGoodsListTask.get());
            entity.put("hotGoodsList", hotGoodsListTask.get());
            entity.put("brandList", brandListTask.get());
            entity.put("topicList", topicListTask.get());
            entity.put("floorGoodsList", floorGoodsListTask.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return entity;
    }


    /*private List getCategoryList() {
        Integer catLogLimit = systemService.getCatLogLimit();
        List<MarketCategory> categoryList = categoryService.queryChannel();
        for (MarketCategory e : categoryList) {
            Integer id = e.getId();
            List<MarketGoods> marketGoods = goodsService.queryByCatId(id);
            //e.setChildren(marketGoods)
        }
        List list = new ArrayList();
        for (int i = 0; i < catLogLimit; i++) {
            list.add(categoryList.get(i));
        }
        return list;
    }*/

    private List<Map> getCategoryList() {
        List<Map> categoryList = new ArrayList<>();
        List<MarketCategory> catL1List = categoryService.queryL1WithoutRecommend(0, systemService.getCatlogListLimit());
        for (MarketCategory catL1 : catL1List) {
            List<MarketCategory> catL2List = categoryService.queryByPid(catL1.getId());
            List<Integer> l2List = new ArrayList<>();
            for (MarketCategory catL2 : catL2List) {
                l2List.add(catL2.getId());
            }

            List<MarketGoods> categoryGoods;
            if (l2List.size() == 0) {
                categoryGoods = new ArrayList<>();
            } else {
                categoryGoods = goodsService.queryByCategory(l2List, 0, systemService.getCatlogMoreLimit());
            }

            Map<String, Object> catGoods = new HashMap<>();
            catGoods.put("id", catL1.getId());
            catGoods.put("name", catL1.getName());
            catGoods.put("goodsList", categoryGoods);
            categoryList.add(catGoods);
        }
        return categoryList;
    }


}
